package com.hongxun.design.jungong.fragment;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.facebook.drawee.view.SimpleDraweeView;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.CreateProjectAttributeCodeActivity;
import com.hongxun.design.activitys.RegeditActivity;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.fragments.BaseFragment;
import com.hongxun.design.jungong.activity.ProRecAndPicActivity;
import com.hongxun.design.jungong.model.RecAndPhoList;
import com.hongxun.design.jungong.model.SsPtType;
import com.hongxun.design.jungong.model.SspPhoto;
import com.hongxun.design.jungong.model.SspRecord;
import com.hongxun.design.jungong.model.SysImgType;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.CheckUtil;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ShardPreferencesTool;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.dialogstyle.MyDialog;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.ui.myiossheet.ActionSheet;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.PopwindowUtil;
import com.hongxun.design.utils.camerautil.CameraUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.fileutil.FileUtil;
import com.hongxun.design.utils.fileutil.SetFileUtil;
import com.hongxun.design.utils.fileutil.ZipUtil;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static android.app.Activity.RESULT_OK;
import static com.hongxun.design.MyApp.activity;

/**
 * Created by cqcqwucp 记录列表显示
 */
public class FragmentPhoto extends BaseFragment implements View.OnClickListener {
    private TextView photoTvValue1;
    private EditText photoEtValue2;
    private LinearLayout photollitem3;
    private ScrollView scrollView;
    private HeadSecondControlPanel headsecond_layout;
    private List<SspRecord> recordList, alllist;
    private ArrayList<String> listType;
    private View view;
    private int position = 0;
    private List<SsPtType> typeList;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            dealwithMsg(msg);
        }
    };

    public FragmentPhoto() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.jg_fragmnet_photo, container, false);
        findViewById();
        return view;
    }

    private void findViewById() {
        initheader();
        ininListView();
    }

    private void sendMsg(final int index, long delay) {
        handler.sendEmptyMessageDelayed(index,delay);
//        new Timer().schedule(new TimerTask() {
//            @Override
//            public void run() {
//                handler.sendEmptyMessage(index);
//            }
//        }, delay);
    }

    //初始化控件
    private void ininListView() {
        scrollView = (ScrollView) view.findViewById(R.id.photo_scrv);
        photollitem3 = (LinearLayout) view.findViewById(R.id.photo_llayout_data);
        view.findViewById(R.id.photo_llitem1).setOnClickListener(this);
        view.findViewById(R.id.photo_ivNewRec).setOnClickListener(this);
        photoTvValue1 = (TextView) view.findViewById(R.id.photo_tvValue1);

        photoTvValue1.setText("全部");
        photoEtValue2 = (EditText) view.findViewById(R.id.photo_etValue2);
        TextView phototvValueIcon1 = (TextView) view.findViewById(R.id.phototvValue_icon1);
        TextView phototvValueIcon2 = (TextView) view.findViewById(R.id.phototvValue_icon2);

        SetTypeface.set(phototvValueIcon1, IconSourse.DROPDOWN);
        SetTypeface.set(phototvValueIcon2, IconSourse.SEARCH);
        phototvValueIcon1.setOnClickListener(this);
        phototvValueIcon2.setOnClickListener(this);

    }

    private void setadapter3(SspRecord record, int index) {
        View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.listview_item_fragphoto, null);
        ViewHolder holder = new ViewHolder(inflate);
        holder.setData(record);
        if (index == 9999) photollitem3.addView(inflate);
        else photollitem3.addView(inflate, index);
    }

    //获取本地数据
    private void tempData() {
        recordList = new ArrayList<>();
        alllist = new ArrayList<>();
        listType = new ArrayList<>();
        typeList = new ArrayList<>();

        String getshare = ShardPreferencesTool.getrecord(getActivity(), MyApp.currProject.getStage_id(), "");
        if (!TextUtils.isEmpty(getshare)) {
            List<SspRecord> records = JSON.parseArray(getshare, SspRecord.class);
            if (records != null) {
                for (SspRecord record : records) {
                    if (!record.getIsdel().equalsIgnoreCase("删除")) {
                        recordList.add(record);
                    }
                }
            }
        }
        alllist.addAll(recordList);
        String typelist = ShardPreferencesTool.getrecord(getActivity(), MString.itl, "");
        List<SsPtType> types = null;
        if (!TextUtils.isEmpty(typelist) && FastJsonUtil.JsonToCheck(typelist)) {
            types = JSON.parseObject(typelist, new TypeReference<SysImgType>() {
            }).photoList;
        }
        listType.add("全部");
        if (types != null && !types.isEmpty()) {
            typeList.addAll(types);
            for (SsPtType ptType : types) {
                listType.add(ptType.getTname());
            }
        }
        photollitem3.removeAllViews();
        for (SspRecord record : recordList) {
            setadapter3(record, 9999);
        }
    }

    //头部控件设置
    private void initheader() {
        headsecond_layout = (HeadSecondControlPanel) view.findViewById(R.id.headsecond_layout);
        String title = MyApp.currProject.getProject_name();
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean(IconSourse.sendup + "",
                View.VISIBLE, R.color.bar_right, ListenerSet.back, title, R.color.title,
                View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
        headsecond_layout.setSearchOnclick(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString(MString.TYPE, MString.projectChoice);
                IntentToActivity.intent(getActivity(), CreateProjectAttributeCodeActivity.class, bundle);
            }
        });
        headsecond_layout.setClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<String> list = new ArrayList<>();
                list.add("同步数据提示");
                list.add("在有网络的情况下\n请保持数据与服务器同步");
                list.add("取消");
                list.add("同步");
                MyDialog.recordDel(getActivity(), list, handler, "", 0, 14);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        String projectName = MyApp.currProject.getProject_name();
        if (!TextUtils.isEmpty(projectName)) {
            headsecond_layout.setSearchTitle(projectName);
            tempData();
        } else {
            headsecond_layout.setSearchTitle("");
        }
    }

    @Override
    public void onClick(View v) {
        if (TextUtils.isEmpty(MyApp.currProject.getProject_name())) {
            ToastShow.showShort(getActivity(), "请先选择项目");
            return;
        }
        switch (v.getId()) {
            case R.id.photo_llitem1:
            case R.id.phototvValue_icon1:
                PopwindowUtil.dropdownpop(getActivity(), listType, v, handler, 7);
                break;
            case R.id.phototvValue_icon2:
                searchData();
                break;
            case R.id.photo_ivNewRec:
                MyDialog.RecordOperation(getActivity(), "新增记录", handler, "", -1, typeList);
                break;
        }
    }

    //关键字搜索
    private void searchData() {
        String c1 = String.valueOf(photoTvValue1.getText().toString().trim());
        String c2 = String.valueOf(photoEtValue2.getText().toString().trim());
        if (TextUtils.isEmpty(c1)) {
            ToastShow.showShort(getActivity(), "请设置记录类型");
            return;
        }
        recordList.clear();
        for (SspRecord record : alllist) {
            String pname = record.getPname();
            String kwords = record.getKwords();
            boolean isT = c1.equalsIgnoreCase("全部") || pname.equalsIgnoreCase(c1);
            if (!TextUtils.isEmpty(c2)) {
                if (kwords.contains(c2) && isT) recordList.add(record);
            } else {
                if (isT) recordList.add(record);
            }
        }
        photollitem3.removeAllViews();
        for (SspRecord record : recordList) {
            setadapter3(record, 9999);
        }
    }

    //照片
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            String path;
            switch (requestCode) {
                case 1://拍照
                    path = Environment.getExternalStorageDirectory() + "/" + Constant.TMP_PATH;
                    savePhotoData(path);
                    break;
                case 2://相册
                    path = CheckUtil.getFilePath(getActivity(), data.getData());
                    savePhotoData(path);
                    break;
            }
        }
    }

    private void savePhotoData(Object... obj) {
        String path = (String) obj[0];
        //跳转到图片水印设计界面
        SspRecord record = recordList.get(position);
        SspPhoto photo = savePhoto(path, record);
        ((ProRecAndPicActivity) getActivity()).onBack(2, record, photo, 0);
    }

    //保存照片数据
    private SspPhoto savePhoto(String path, SspRecord record) {
        String millis = String.valueOf(System.currentTimeMillis());
        SspPhoto photo = new SspPhoto();
        photo.setId(millis);
        photo.setGuid(MString.getMyUUID());
        photo.setSid(MyApp.currProject.getStage_id());
        photo.setRguid(record.getGuid());
        //下一界面设置数据
//        photo.setPlistone(record.getPlist());
//        photo.setRemarks("暂无，未设置");

        photo.setLongitude(String.valueOf(MyApp.bdLocation.getLongitude()));
        photo.setLatitude(String.valueOf(MyApp.bdLocation.getLatitude()));
        photo.setFounderid(MyApp.userInfo.getUser_id());
        photo.setFoundertime(millis);
        String name = path.substring(path.lastIndexOf("/") + 1);
        photo.setImgname(name);
        photo.setImgaddress(path);
        photo.setImgurl("");
        photo.setIsdel("新增");
        photo.setLasttime(millis);

        return photo;
    }

    //保存数据
    private void saveToXml(String json, String key) {
        ShardPreferencesTool.saverecord(getActivity(), key, json);
    }

    class ViewHolder {
        private TextView tv1, tv2, tv3, tv4, tv5, tv6;
        private SimpleDraweeView iv1, iv2, iv3, iv4;
        private LinearLayout layout;
        private List<SimpleDraweeView> views;
        private List<TextView> textViewList;

        ViewHolder(View view) {
            view.setTag(this);
            finByIds(view);
        }

        private void finByIds(View view) {
            layout = (LinearLayout) view.findViewById(R.id.fragphoto_llayout);

            tv1 = (TextView) view.findViewById(R.id.fragphoto_keywords);
            tv2 = (TextView) view.findViewById(R.id.fragphoto_tvdel);
            tv3 = (TextView) view.findViewById(R.id.fragphoto_tvedit);
            tv4 = (TextView) view.findViewById(R.id.fragphoto_tvpz);
            tv5 = (TextView) view.findViewById(R.id.fragphoto_type);
            tv6 = (TextView) view.findViewById(R.id.fragphoto_num);

            textViewList = new ArrayList<>();
            textViewList.add(tv2);
            textViewList.add(tv3);
            textViewList.add(tv4);

            iv1 = (SimpleDraweeView) view.findViewById(R.id.photoiv1);
            iv2 = (SimpleDraweeView) view.findViewById(R.id.photoiv2);
            iv3 = (SimpleDraweeView) view.findViewById(R.id.photoiv3);
            iv4 = (SimpleDraweeView) view.findViewById(R.id.photoiv4);

            views = new ArrayList<>();
            views.add(iv1);
            views.add(iv2);
            views.add(iv3);
            views.add(iv4);
        }

        public void setData(SspRecord obj) {
            final List<SspPhoto> photoListY = getphotoListByRec(obj);
            tv1.setText(obj.getPileseat() + "\t" + obj.getKwords());
            tv5.setText(obj.getPname());
            tv6.setText(photoListY.size() + "");
            layout.setOnClickListener(listener(obj));

            for (TextView view : textViewList) {
                view.setTag("0");
                view.setOnClickListener(listener(obj));
            }
            for (SimpleDraweeView view : views) {
                view.setOnClickListener(listener(obj));
            }
            for (int i = 0; i < photoListY.size(); i++) {
                if (i < 4) {
                    String url = photoListY.get(i).getImgaddress();
                    if (null == url) url = "0";
                    if (!new File(url).exists()) url = photoListY.get(i).getImgurl();
                    if (null == url) views.get(i).setImageResource(R.mipmap.zhanweibanner);
                    else {
                        views.get(i).setImageURI(UriUtil.getUri(url));
                        views.get(i).setTag(url);
                    }
                }
            }
        }

        private View.OnClickListener listener(final SspRecord obj) {
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.fragphoto_llayout:
                            Message message = handler.obtainMessage();
                            message.obj = obj;
                            message.what = 10;
                            message.arg1 = recordList.indexOf(obj);
                            handler.sendMessage(message);
                            break;
                        case R.id.fragphoto_tvdel:
                            List<String> list = new ArrayList<>();
                            list.add("删除提示");
                            list.add("是否删除记录\n\n" + obj.getPileseat() + "\t" + obj.getKwords());
                            list.add("否");
                            list.add("是");
                            MyDialog.recordDel(getActivity(), list, handler, obj, 0, 3);
                            break;
                        case R.id.fragphoto_tvedit:
                            Message msg = handler.obtainMessage();
                            msg.obj = obj;
                            msg.what = 2;
                            handler.sendMessage(msg);
                            break;
                        case R.id.fragphoto_tvpz://拍照
                            Message msg8 = handler.obtainMessage();
                            msg8.obj = obj;
                            msg8.what = 8;
                            msg8.arg1 = recordList.indexOf(obj);
                            handler.sendMessage(msg8);
                            openSheet(getActivity(), "选择方式", "相册", "拍照");
                            break;
                        case R.id.photoiv1:
                        case R.id.photoiv2:
                        case R.id.photoiv3:
                        case R.id.photoiv4:
                            if (null == v.getTag() || String.valueOf(v.getTag()).equalsIgnoreCase("0")) {
                                return;
                            }
                            Message msgiv = handler.obtainMessage();
                            msgiv.what = 4;
                            msgiv.obj = v.getTag();//图片url
                            msgiv.arg1 = recordList.indexOf(obj);
                            if (v.getId() == R.id.photoiv1) msgiv.arg2 = 0;
                            else if (v.getId() == R.id.photoiv2) msgiv.arg2 = 1;
                            else if (v.getId() == R.id.photoiv3) msgiv.arg2 = 2;
                            else if (v.getId() == R.id.photoiv4) msgiv.arg2 = 3;
                            handler.sendMessage(msgiv);
                            break;
                    }
                }
            };
            return listener;
        }

    }

    private List<SspPhoto> getphotoListByRec(SspRecord obj) {
        List<SspPhoto> photoListY = new ArrayList<>();
        String photots = ShardPreferencesTool.getrecord(getActivity(), obj.getGuid(), "");
        List<SspPhoto> photoList = JSON.parseArray(photots, SspPhoto.class);
        if (photoList != null)
            for (SspPhoto photo : photoList) {
                if (!photo.getIsdel().equalsIgnoreCase("删除")) photoListY.add(photo);
            }
        return photoListY;
    }

    private void openSheet(Activity activity, final String... titles) {
        activity.setTheme(R.style.ActionSheetStyleIOS7);
        ActionSheet menuView = new ActionSheet(activity);
        menuView.setCancelButtonTitle("取消");// before add items
        menuView.addItems(titles);
        menuView.setItemClickListener(new ActionSheet.MenuItemClickListener() {
            @Override
            public void onItemClick(int itemPosition) {
                if (itemPosition == 0) return;
                if (itemPosition == 1) {
                    CameraUtil.pickPhoto(RegeditActivity.Code_Select);//相册
                } else {
                    CameraUtil.openCamera(RegeditActivity.Code_Pick);//拍照
                }
            }
        });
        menuView.setCancelableOnTouchMenuOutside(true);
        menuView.showMenu();
    }

    private void dealwithMsg(Message msg) {
        switch (msg.what) {
            case 1://下拉加载数据
                ToastShow.showShort(getActivity(), String.valueOf(msg.obj));
                break;
            case 2://修改记录
                int indexOf = recordList.indexOf(msg.obj);
                MyDialog.RecordOperation(getActivity(), "修改记录", handler, msg.obj, indexOf, typeList);
                break;
            case 3://删除记录
                delRecord(msg);
                break;
            case 4://点击看大图
                SspRecord record = recordList.get(msg.arg1);
                MyDialog.showBigImgDialog(getActivity(), record, msg);
                break;
            case 5://保存
            case 6://拍照
                savePhotoDataAndPic56(msg);
                break;
            case 7://设置记录类型
                photoTvValue1.setText(listType.get(msg.arg1));
                searchData();
                break;
            case 8://记录拍照item位置
                position = msg.arg1;
                break;
            case 9:
                scrollView.fullScroll(ScrollView.FOCUS_UP);//滚动到顶部
                break;
            case 10://显示listview数据
                SspRecord record10 = recordList.get(msg.arg1);
                ((ProRecAndPicActivity) getActivity()).onBack(1, record10);
                break;
            case 13://下载数据
                DateFormat format13 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long lastime13 = 0;
                try {
                    lastime13 = format13.parse("2000-01-01 00:00:00").getTime();
                } catch (ParseException e) {
                    //默认1年前吧
                    Calendar calendar = Calendar.getInstance();
                    Date date = new Date(System.currentTimeMillis());
                    calendar.setTime(date);
                    calendar.add(Calendar.YEAR, -1);
                    lastime13 = calendar.getTime().getTime();
                }
//                String lasttime = ShardPreferencesTool.getrecord(getActivity(), "REC_lt", String.valueOf(lastime13));
                MyApp.HTTP.getProRecordList(handler, 15, MyApp.currProject.getStage_id(), String.valueOf(lastime13));
                break;
            case 14://提交项目记录数据
                showWaitDialog(-1, "处理中...");
                try {
                    List<SspPhoto> photos = new ArrayList<>();
                    List<SspRecord> sspRecords = new ArrayList<>();
                    String rec = ShardPreferencesTool.getrecord(getActivity(), MyApp.currProject.getStage_id(), "");
                    List<SspRecord> records = JSON.parseArray(rec, SspRecord.class);
                    if (records != null) {
                        for (SspRecord sspRecord : records) {
                            if (!sspRecord.getIsdel().equalsIgnoreCase("已同步")) {
                                sspRecords.add(sspRecord);//上传的数据
                                String pho = ShardPreferencesTool.getrecord(getActivity(), sspRecord.getGuid(), "");
                                List<SspPhoto> sspPhotos = JSON.parseArray(pho, SspPhoto.class);
                                if (sspPhotos != null) {
                                    for (SspPhoto photo : sspPhotos) {
                                        if (!photo.getIsdel().equalsIgnoreCase("已同步")) {
                                            photos.add(photo);//上传的数据
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //最新上传文件录入数据
                    RecAndPhoList list = new RecAndPhoList();
                    list.setRecordList(sspRecords);
                    list.setPhotoList(photos);
                    String toJson = FastJsonUtil.beanToJson(list);
                    String idType = SetFileUtil.CheckStageId_type();
                    //
                    String currPath = MyApp.File.getCachaPhotoPath_Record();
                    String name = MString.projectStagePhInfo_ + idType + ".tio";
                    String filepath = currPath + "/" + name;
                    if (new File(filepath).exists()) {
                        FileUtil.deleteFile(filepath);
                    }
                    String dir = idType + "/" + MString.ProjectRecord + "/Photo_" + idType;
                    MyApp.File.SaveFileDataAll(dir, name, toJson);
                    //zip照片及数据
                    uppath = ZipUtil.zipFolder(currPath);
                } catch (Exception e) {
                    ToastShow.showShort(activity, e.toString());
                    hideWaitIngDialog();
                } finally {
                    sendMsg(19, 1500);
                }
                break;
            case 15:
                String vf15 = String.valueOf(msg.obj);
                if (vf15.contains("\"code\"") || vf15.contains("\"msg\"")) {
                    ToastShow.showShort(getActivity(), FastJsonUtil.JsonToCheck_String(vf15));
                    return;
                } else {
                    showWaitDialog(-1, "处理中...");
                    dealwithMsg15(vf15);
                }
                break;
            case 16:
                hideWaitIngDialog();
                break;
            case 17://上传数据
                uploadProRecFile17(msg);
                break;
            case 18://上传结果处理
                hideWaitIngDialog();
                handler.sendEmptyMessage(13);//下载数据
                break;
            case 19:
                MyApp.HTTP.checkStageRec(handler, 17, SetFileUtil.CheckStageId_type());
                break;
        }
    }

    private String uppath = "";

    private void uploadProRecFile17(Message msg) {
        if (FastJsonUtil.JsonToCheck((String) msg.obj)) {
            MyApp.HTTP.uploadProRecAndPic(handler, 18, new File(uppath), SetFileUtil.CheckStageId_type());
        } else {
            handler.sendEmptyMessage(16);
            ToastShow.showShort(getActivity(), FastJsonUtil.JsonToCheck_String(String.valueOf(msg.obj)));
            MyDialog.dialogFordelectCacheProject(activity, MyApp.currProject, FastJsonUtil.JsonToCheck_String((String) msg.obj));
        }
    }

    private void dealwithMsg15(String vf15) {
        long lastime15 = System.currentTimeMillis();
        ShardPreferencesTool.saverecord(getActivity(), "REC_lt", lastime15);
        RecAndPhoList infoBean = JSON.parseObject(vf15, new TypeReference<RecAndPhoList>() {
        });
        List<SspPhoto> photos = new ArrayList<>();
        List<SspPhoto> photosIns = new ArrayList<>();
        List<SspRecord> records = new ArrayList<>();
        List<SspRecord> recordsIns = new ArrayList<>();
        if (null != infoBean.recordList) {
            records.addAll(infoBean.recordList);
        }
        if (null != infoBean.photoList) {
            photos.addAll(infoBean.photoList);
        }
        //增量数据处理
        for (SspRecord record : records) {
            String guid = record.getGuid();
            if (null != record.getIsdel() && record.getIsdel().equalsIgnoreCase("删除")) {
                ShardPreferencesTool.delrecord(getActivity(), guid);//删除照片数据
            } else {
                record.setIsdel("已同步");
                record.setLasttime(String.valueOf(lastime15));
                recordsIns.add(record);//去掉"删除"状态数据
                for (SspPhoto photo : photos) {
                    if (photo.getRguid().equalsIgnoreCase(guid) && photo.getIsdel() != null) {
                        if (photo.getIsdel().equalsIgnoreCase("删除")) {
                            FileUtil.deleteFile(photo.getImgaddress());//删除照片文件
                        } else {
                            if (null != photo.getImgurl()) {
                                Uri uri = UriUtil.getImgFromService(photo.getImgurl());//下载照片
                                photo.setImgaddress(uri.getPath());
                            }
                            photo.setIsdel("已同步");
                            photo.setLasttime(String.valueOf(lastime15));
                            photosIns.add(photo);//保存到本地照片数据
                        }
                    }
                }
                saveToXml(FastJsonUtil.beanToJson(photosIns), guid);
            }
        }
        saveToXml(FastJsonUtil.beanToJson(recordsIns), MyApp.currProject.getStage_id());
        tempData();
        sendMsg(16, 1000);
    }

    private void delRecord(Message msg) {
        //界面变化
        SspRecord record3 = (SspRecord) msg.obj;
        alllist.remove(record3);
        int of = recordList.indexOf(record3);
        photollitem3.removeViewAt(of);
        Toast.makeText(getActivity(), "删除成功！", Toast.LENGTH_SHORT).show();
        //修改记录
        if (record3.getIsdel().equalsIgnoreCase("已同步")) {
            record3.setIsdel("删除");
            recordList.set(of, record3);
        } else {
            recordList.remove(record3);
        }
        saveToXml(FastJsonUtil.beanToJson(recordList), MyApp.currProject.getStage_id());
        //修改照片
        String s = ShardPreferencesTool.getrecord(getActivity(), record3.getGuid(), "");
        List<SspPhoto> photos = JSON.parseArray(s, SspPhoto.class);
        List<SspPhoto> photosL = new ArrayList<>();
        if (photos != null) {
            for (SspPhoto photo : photos) {
                if (photo.getIsdel().equalsIgnoreCase("已同步")) photo.setIsdel("删除");
                photosL.add(photo);
                FileUtil.deleteFile(photo.getImgaddress());
            }
            saveToXml(FastJsonUtil.beanToJson(photosL), record3.getGuid());
        }
    }

    private void savePhotoDataAndPic56(Message msg) {
        Bundle data = msg.getData();
        SsPtType type56 = (SsPtType) data.getSerializable("0");
        String str1 = data.getString("1");
        String str2 = data.getString("2");
        String str3 = data.getString("3");
        SspRecord record56;
        long millis = System.currentTimeMillis();
        if (msg.arg1 == -1) {
            record56 = new SspRecord(str1, str2);
            record56.setId(String.valueOf(millis));
            record56.setGuid(MString.getMyUUID());
            record56.setSid(MyApp.currProject.getStage_id());
            record56.setPid(type56.getId());
            record56.setPlist(type56.getTplist());//这里存入逗号分隔的数据
            record56.setFounderid(MyApp.userInfo.getUser_id());
            record56.setFoundertime(String.valueOf(millis));
            record56.setPileseat(str3);
            record56.setRemarks("暂不使用");
            record56.setIsdel("新增");
            record56.setLasttime(String.valueOf(millis));
            alllist.add(0, record56);
        } else {
            record56 = recordList.get(msg.arg1);
            record56.setPname(str1);
            record56.setKwords(str2);
            record56.setPid(type56.getId());
            record56.setPlist(type56.getTplist());
            record56.setPileseat(str3);
            record56.setLasttime(String.valueOf(millis));
            record56.setIsdel("修改");
            photollitem3.removeViewAt(msg.arg1);
            recordList.remove(msg.arg1);
            alllist.remove(msg.arg1);
            alllist.add(0, record56);
        }
        setadapter3(record56, 0);
        recordList.add(0, record56);
        saveToXml(FastJsonUtil.beanToJson(recordList), MyApp.currProject.getStage_id());
        sendMsg(9, 500);
        //拍照
        if (msg.what == 6) {
            openSheet(getActivity(), "选择方式", "相册", "拍照");
        }
    }
}
